/*******************************************************************************
 * Copyright (c) 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.search;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.search.ui.ISearchPageContainer;
import org.eclipse.search.ui.ISearchQuery;
import org.eclipse.search.ui.ISearchResult;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.core.history.IFileHistory;
import org.eclipse.team.core.history.IFileHistoryProvider;
import org.eclipse.team.core.history.IFileRevision;

final class SearchQuery implements ISearchQuery {
	
	private String text;
	private ISearchPageContainer container;
	private SearchResult searchResult= new SearchResult(this);

	public SearchQuery(String text, ISearchPageContainer container) {
		this.text = text;
		this.container = container;
	}

	public boolean canRerun() {
		return false; /* for now */
	}

	public boolean canRunInBackground() {
		return true;
	}

	public String getLabel() {
		return "CVS Search result";
	}

	public ISearchResult getSearchResult() {
		return searchResult;
	}

	public IStatus run(final IProgressMonitor monitor)
			throws OperationCanceledException {
		switch(container.getSelectedScope()){
		case ISearchPageContainer.WORKSPACE_SCOPE:
			IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
			IProject[] projects = root.getProjects();
			for(int i = 0; i < projects.length; i++){
				try {
					RepositoryProvider rp = RepositoryProvider.getProvider(projects[i]);
					if(rp == null){
						// project is not shared
						continue;
					}
					final IFileHistoryProvider fhp = rp.getFileHistoryProvider();
					if(fhp == null){
						//no file history provider, nothing to do
						// TODO: log this
						continue;
					}
					projects[i].accept(new IResourceVisitor(){

						public boolean visit(IResource resource)
								throws CoreException {
							//TODO: take care about monitors!
							IFileHistory fh = fhp.getFileHistoryFor(resource, IFileHistoryProvider.NONE, monitor);
							if(fh != null){
								IFileRevision[] revisions = fh.getFileRevisions();
								for (int j = 0; j < revisions.length; j++) {
									String comment = revisions[j].getComment();
									//TODO: add proper matching
									if(comment != null && comment.matches(".*" + text + ".*")){
										searchResult.add(revisions[j]);
									}

								}
							}
							if(resource.getType() == IResource.FILE){
								return false;
							}
							return true;
						}
						
					});
				} catch (CoreException e) {
					e.printStackTrace();
				}
			}
			break;
		case ISearchPageContainer.WORKING_SET_SCOPE:
		case ISearchPageContainer.SELECTED_PROJECTS_SCOPE:
		case ISearchPageContainer.SELECTION_SCOPE:
		}
		return Status.OK_STATUS;
	}
}